package com.gt.aspect;

import com.alibaba.fastjson.JSONObject;
import com.gt.annotation.DecryptId;
import com.gt.annotation.RedisLock;
import com.gt.utils.DESUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * DES解密id AOP
 *
 * @author linc
 * @date 2024/06/18
 */
@Slf4j
@Aspect
@Component
@Order(6)
public class DecryptIdAop {

//    @Pointcut(value = "@annotation(com.gt.annotation.DecryptId) ")
//    public void pointCut() {
//    }

//    @Around("pointCut()")
    @Around("@annotation(decryptId)")
    public Object doAround(ProceedingJoinPoint joinPoint,DecryptId decryptId) throws Throwable {
        Object[] args= joinPoint.getArgs();
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations();
//        for (Annotation[] parameterAnnotation : parameterAnnotations) {
//            int paramIndex = ArrayUtils.indexOf(parameterAnnotations, parameterAnnotation);
//            for (Annotation annotation : parameterAnnotation) {
//                if (annotation instanceof DecryptId) {
//                    String keyName = ((DecryptId)annotation).keyName();
                    String keyName = decryptId.keyName();
            // 获取reqJson
                    args = joinPoint.getArgs();
                    JSONObject reqJson = (JSONObject) args[0];

                    // 指定值解析id并重新赋值
                    String[] keyNameArr = keyName.split(",");
                    for (String str : keyNameArr) {
                        Object keyValue = reqJson.get(str);
                        if (keyValue != null) {
                            String decrypt = DESUtil.encrypt(DESUtil.DES_KEY,keyValue.toString());
                            reqJson.put(str, decrypt);
                        }
                    }
                    // 将修改好的参数放进目标函数的参数列表
                    return joinPoint.proceed(args);
//                }
//            }
//        }
//        return joinPoint.proceed(args);
    }
}
